Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 723)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02084 13.01766 13.01452 13.01144 13.00839 13.00539 13.00242 12.99949
##   [9] 12.99660 12.99373 12.99090 12.98809 12.98530 12.98254 12.97979 12.97706
##  [17] 12.97435 12.97164 12.96894 12.96625 12.96356 12.96087 12.95819 12.95549
##  [25] 12.95279 12.95008 12.94736 12.94463 12.94188 12.93911 12.93631 12.93350
##  [33] 12.93065 12.92778 12.92487 12.92193 12.91896 12.91594 12.91289 12.90981
##  [41] 12.90671 12.90358 12.90044 12.89728 12.89412 12.89095 12.88777 12.88460
##  [49] 12.88144 12.87829 12.87516 12.87204 12.86895 12.86589 12.86285 12.85986
##  [57] 12.85690 12.85399 12.85112 12.84831 12.84555 12.84285 12.84022 12.83766
##  [65] 12.83517 12.83275 12.83042 12.82817 12.82601 12.82394 12.82197 12.82010
##  [73] 12.81833 12.81668 12.81514 12.81371 12.81241 12.81123 12.80996 12.80840
##  [81] 12.80655 12.80444 12.80208 12.79949 12.79670 12.79372 12.79056 12.78725
##  [89] 12.78380 12.78024 12.77657 12.77282 12.76901 12.76515 12.76126 12.75736
##  [97] 12.75347 12.74961 12.74579 12.74203 12.73836 12.73478 12.73132 12.72799
## [105] 12.72481 12.72181 12.71900 12.71639 12.71400 12.71186 12.70998 12.70838
## [113] 12.70708 12.70609 12.70544 12.70513 12.70520 12.70566 12.70651 12.70780
## [121] 12.70976 12.71260 12.71624 12.72061 12.72564 12.73127 12.73742 12.74402
## [129] 12.75100 12.75828 12.76581 12.77351 12.78130 12.78912 12.79689 12.80456
## [137] 12.81204 12.81926 12.82616 12.83266 12.83869 12.84419 12.85070 12.85968
## [145] 12.87088 12.88403 12.89889 12.91520 12.93273 12.95120 12.97039 12.99002
## [153] 13.00985 13.02964 13.04912 13.06805 13.08617 13.10324 13.11900 13.13321
## [161] 13.14561 13.15594 13.16397 13.17124 13.17945 13.18850 13.19832 13.20881
## [169] 13.21991 13.23151 13.24354 13.25592 13.26857 13.28139 13.29431 13.30724
## [177] 13.32011 13.33282 13.34529 13.35745 13.36920 13.38046 13.39116 13.40120
## [185] 13.41051 13.41900 13.42658 13.43318 13.43871 13.44309 13.44623 13.44805
## [193] 13.44847 13.44741 13.44478 13.44049 13.43448 13.42681 13.41759 13.40695
## [201] 13.39498 13.38181 13.36756 13.35234 13.33626 13.31944 13.30200 13.28405
## [209] 13.26570 13.24708 13.22829 13.20946 13.19069 13.17211 13.15382 13.13595
## [217] 13.11861 13.09913 13.07518 13.04736 13.01629 12.98255 12.94676 12.90953
## [225] 12.87145 12.83314 12.79519 12.75822 12.72282 12.68961 12.65918 12.63215
## [233] 12.60912 12.58725 12.56346 12.53798 12.51104 12.48285 12.45365 12.42366
## [241] 12.39312 12.36224 12.33126 12.30039 12.26988 12.23994 12.21080 12.18269
## [249] 12.15584 12.13046 12.10680 12.08507 12.06550 12.04832 12.03355 12.02091
## [257] 12.01023 12.00132 11.99399 11.98806 11.98335 11.97966 11.97682 11.97464
## [265] 11.97292 11.97150 11.97017 11.96876 11.96708 11.96495 11.96218 11.95858
## [273] 11.95397 11.94816 11.94097 11.93372 11.92774 11.92291 11.91909 11.91614
## [281] 11.91394 11.91235 11.91122 11.91044 11.90986 11.90936 11.90878 11.90802
## [289] 11.90691 11.90534 11.90317 11.90027 11.89649 11.89171 11.88579 11.87860
## [297] 11.87064 11.86254 11.85430 11.84593 11.83745 11.82887 11.82020 11.81145
## [305] 11.80264 11.79377 11.78486 11.77592 11.76697 11.75801 11.74906 11.74013
## [313] 11.73123 11.72237 11.71357 11.70323 11.68995 11.67404 11.65584 11.63566
## [321] 11.61381 11.59063 11.56642 11.54151 11.51621 11.49085 11.46574 11.44121
## [329] 11.41757 11.39514 11.37425 11.35521 11.33833 11.32395 11.31238 11.30393
## [337] 11.29894 11.29524 11.29066 11.28544 11.27981 11.27403 11.26833 11.26296
## [345] 11.25816 11.25418 11.25126 11.24964 11.24957 11.25129 11.25505 11.26108
## [353] 11.26967 11.28081 11.29435 11.31012 11.32796 11.34769 11.36916 11.39220
## [361] 11.41664 11.44233 11.46908 11.49675 11.52516 11.55416 11.58357 11.61322
## [369] 11.64297 11.67263 11.70205 11.73106 11.75949 11.78719 11.81398 11.83970
## [377] 11.86419 11.88728 11.91212 11.94153 11.97485 12.01142 12.05056 12.09163
## [385] 12.13395 12.17686 12.21969 12.26179 12.30249 12.34112 12.37703 12.40954
## [393] 12.43800 12.46173 12.48388 12.50784 12.53336 12.56018 12.58807 12.61676
## [401] 12.64601 12.67557 12.70518 12.73459 12.76355 12.79182 12.81914 12.84526
## [409] 12.86992 12.89289 12.91390 12.93270 12.94906 12.96270 12.97339 12.98121
## [417] 12.98654 12.98958 12.99052 12.98955 12.98686 12.98265 12.97712 12.97044
## [425] 12.96283 12.95446 12.94554 12.93626 12.92680 12.91737 12.90815 12.89934
## [433] 12.89113 12.88372 12.87730 12.87205 12.86588 12.85674 12.84490 12.83066
## [441] 12.81430 12.79610 12.77635 12.75533 12.73332 12.71062 12.68750 12.66425
## [449] 12.64116 12.61850 12.59656 12.57563 12.55599 12.53792 12.52172 12.50766
## [457] 12.49602 12.48497 12.47257 12.45901 12.44447 12.42913 12.41317 12.39676
## [465] 12.38009 12.36334 12.34669 12.33031 12.31439 12.29910 12.28463 12.27115
## [473] 12.25885 12.24791 12.23849 12.23079 12.22393 12.21694 12.20989 12.20284
## [481] 12.19584 12.18895 12.18223 12.17574 12.16953 12.16367 12.15822 12.15323
## [489] 12.14876 12.14486 12.14161 12.13905 12.13725 12.13626 12.13614 12.13695
## [497] 12.13875 12.14213 12.14753 12.15477 12.16366 12.17401 12.18565 12.19839
## [505] 12.21204 12.22642 12.24135 12.25665 12.27212 12.28759 12.30287 12.31777
## [513] 12.33213 12.34574 12.35842 12.37000 12.38029 12.38910 12.39863 12.41098
## [521] 12.42582 12.44284 12.46170 12.48210 12.50371 12.52620 12.54926 12.57255
## [529] 12.59577 12.61858 12.64067 12.66171 12.68138 12.69935 12.71532 12.72894
## [537] 12.73991 12.74790 12.75259 12.75575 12.75934 12.76324 12.76739 12.77169
## [545] 12.77605 12.78038 12.78459 12.78859 12.79230 12.79563 12.79848 12.80077
## [553] 12.80241 12.80330 12.80337 12.80252 12.80066 12.79771 12.79357 12.78816
## [561] 12.78138 12.77315 12.76256 12.74895 12.73260 12.71379 12.69281 12.66994
## [569] 12.64547 12.61967 12.59282 12.56522 12.53714 12.50886 12.48067 12.45286
## [577] 12.42569 12.39947 12.37446 12.35095 12.32923 12.30958 12.29227 12.27422
## [585] 12.25242 12.22727 12.19918 12.16855 12.13581 12.10135 12.06559 12.02894
## [593] 11.99179 11.95457 11.91767 11.88152 11.84651 11.81305 11.78156 11.75244
## [601] 11.72610 11.70295 11.68340 11.66785 11.65466 11.64192 11.62965 11.61787
## [609] 11.60661 11.59588 11.58570 11.57610 11.56710 11.55871 11.55097 11.54389
## [617] 11.53749 11.53179 11.52682 11.52259 11.51913 11.51645 11.51459 11.51356
## [625] 11.51337 11.51503 11.51934 11.52606 11.53494 11.54574 11.55821 11.57210
## [633] 11.58717 11.60317 11.61985 11.63698 11.65430 11.67157 11.68854 11.70496
## [641] 11.72060 11.73520 11.74853 11.76032 11.77194 11.78481 11.79883 11.81386
## [649] 11.82978 11.84648 11.86382 11.88169 11.89996 11.91852 11.93723 11.95598
## [657] 11.97465 11.99311 12.01124 12.02892 12.04603 12.06244 12.07803 12.09269
## [665] 12.10628 12.11946 12.13296 12.14674 12.16076 12.17500 12.18943 12.20401
## [673] 12.21872 12.23352 12.24838 12.26327 12.27815 12.29300 12.30779 12.32248
## [681] 12.33704 12.35145 12.36566 12.37966 12.39340 12.40685 12.42018 12.43355
## [689] 12.44696 12.46040 12.47387 12.48735 12.50084 12.51434 12.52783 12.54132
## [697] 12.55478 12.56823 12.58164 12.59502 12.60835 12.62163 12.63485 12.64801
## [705] 12.66110 12.67411 12.68703 12.69982 12.71246 12.72495 12.73730 12.74953
## [713] 12.76166 12.77369 12.78563 12.79751 12.80933 12.82111 12.83286 12.84459
## [721] 12.85631 12.86805 12.87980
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 723)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62493 12.62040 12.61596 12.61163 12.60738 12.60324 12.59918 12.59521
##   [9] 12.59134 12.58754 12.58384 12.58021 12.57667 12.57320 12.56982 12.56651
##  [17] 12.56327 12.56010 12.55700 12.55398 12.55102 12.54812 12.54529 12.54251
##  [25] 12.53980 12.53714 12.53454 12.53199 12.52950 12.52705 12.52466 12.52231
##  [33] 12.52000 12.51774 12.51552 12.51333 12.51118 12.50904 12.50693 12.50485
##  [41] 12.50279 12.50077 12.49879 12.49685 12.49495 12.49311 12.49131 12.48958
##  [49] 12.48790 12.48628 12.48474 12.48326 12.48186 12.48054 12.47931 12.47815
##  [57] 12.47709 12.47612 12.47525 12.47447 12.47381 12.47325 12.47280 12.47247
##  [65] 12.47225 12.47216 12.47220 12.47236 12.47266 12.47310 12.47368 12.47441
##  [73] 12.47528 12.47630 12.47748 12.47883 12.48033 12.48200 12.48381 12.48573
##  [81] 12.48776 12.48989 12.49212 12.49446 12.49691 12.49945 12.50210 12.50484
##  [89] 12.50769 12.51063 12.51367 12.51680 12.52003 12.52336 12.52678 12.53028
##  [97] 12.53388 12.53757 12.54135 12.54522 12.54917 12.55321 12.55733 12.56154
## [105] 12.56583 12.57020 12.57465 12.57918 12.58379 12.58848 12.59325 12.59809
## [113] 12.60301 12.60800 12.61306 12.61819 12.62340 12.62867 12.63401 12.63942
## [121] 12.64500 12.65084 12.65690 12.66318 12.66963 12.67625 12.68300 12.68987
## [129] 12.69682 12.70384 12.71090 12.71798 12.72505 12.73209 12.73908 12.74600
## [137] 12.75281 12.75950 12.76604 12.77241 12.78006 12.79029 12.80284 12.81745
## [145] 12.83386 12.85180 12.87102 12.89126 12.91225 12.93373 12.95544 12.97713
## [153] 12.99853 13.01937 13.03941 13.05837 13.07600 13.09204 13.10622 13.11829
## [161] 13.12798 13.13711 13.14760 13.15930 13.17207 13.18579 13.20031 13.21550
## [169] 13.23122 13.24733 13.26370 13.28020 13.29668 13.31300 13.32904 13.34466
## [177] 13.35971 13.37407 13.38759 13.40014 13.41158 13.42178 13.43061 13.43791
## [185] 13.44356 13.44742 13.44936 13.44923 13.44690 13.44234 13.43569 13.42710
## [193] 13.41671 13.40467 13.39112 13.37620 13.36005 13.34282 13.32465 13.30568
## [201] 13.28607 13.26594 13.24545 13.22474 13.20394 13.18321 13.16269 13.14252
## [209] 13.12284 13.10380 13.08554 13.06820 13.04930 13.02656 13.00043 12.97133
## [217] 12.93972 12.90604 12.87072 12.83422 12.79696 12.75941 12.72199 12.68515
## [225] 12.64933 12.61498 12.58253 12.55243 12.52512 12.50104 12.48063 12.46159
## [233] 12.44141 12.42024 12.39825 12.37557 12.35237 12.32879 12.30500 12.28114
## [241] 12.25737 12.23384 12.21070 12.18811 12.16621 12.14518 12.12515 12.10628
## [249] 12.08872 12.07263 12.05815 12.04545 12.03489 12.02657 12.02032 12.01594
## [257] 12.01323 12.01200 12.01205 12.01320 12.01524 12.01798 12.02123 12.02479
## [265] 12.02847 12.03207 12.03541 12.03828 12.04049 12.04185 12.04216 12.04122
## [273] 12.03886 12.03697 12.03746 12.04005 12.04448 12.05050 12.05784 12.06625
## [281] 12.07547 12.08523 12.09527 12.10534 12.11517 12.12451 12.13309 12.14065
## [289] 12.14694 12.15170 12.15465 12.15555 12.15414 12.15015 12.14427 12.13743
## [297] 12.12974 12.12130 12.11220 12.10255 12.09245 12.08201 12.07131 12.06048
## [305] 12.04959 12.03877 12.02811 12.01770 12.00766 11.99809 11.98705 11.97279
## [313] 11.95566 11.93599 11.91413 11.89043 11.86523 11.83887 11.81170 11.78406
## [321] 11.75630 11.72876 11.70178 11.67572 11.65091 11.62769 11.60642 11.58744
## [329] 11.57109 11.55771 11.54765 11.53840 11.52741 11.51495 11.50130 11.48674
## [337] 11.47155 11.45601 11.44039 11.42497 11.41004 11.39587 11.38273 11.37092
## [345] 11.36070 11.35235 11.34615 11.34239 11.34133 11.34326 11.34796 11.35492
## [353] 11.36398 11.37499 11.38777 11.40218 11.41805 11.43522 11.45353 11.47283
## [361] 11.49295 11.51374 11.53503 11.55667 11.57849 11.60034 11.62205 11.64348
## [369] 11.66445 11.68480 11.70439 11.72305 11.74315 11.76694 11.79401 11.82397
## [377] 11.85641 11.89095 11.92718 11.96471 12.00315 12.04208 12.08113 12.11989
## [385] 12.15796 12.19495 12.23046 12.26410 12.29546 12.32416 12.34978 12.37195
## [393] 12.39311 12.41585 12.43997 12.46524 12.49147 12.51844 12.54594 12.57376
## [401] 12.60169 12.62953 12.65706 12.68407 12.71036 12.73571 12.75991 12.78276
## [409] 12.80404 12.82355 12.84107 12.85640 12.86933 12.88072 12.89157 12.90188
## [417] 12.91163 12.92080 12.92938 12.93736 12.94471 12.95144 12.95752 12.96293
## [425] 12.96767 12.97172 12.97506 12.97769 12.97958 12.98072 12.98110 12.98071
## [433] 12.97952 12.97753 12.97472 12.96979 12.96163 12.95055 12.93686 12.92088
## [441] 12.90289 12.88322 12.86217 12.84005 12.81716 12.79381 12.77030 12.74695
## [449] 12.72407 12.70195 12.68090 12.66124 12.64327 12.62730 12.61363 12.59938
## [457] 12.58178 12.56135 12.53860 12.51403 12.48816 12.46149 12.43453 12.40779
## [465] 12.38178 12.35701 12.33398 12.31321 12.29520 12.28047 12.26951 12.25995
## [473] 12.24916 12.23734 12.22464 12.21125 12.19734 12.18310 12.16869 12.15429
## [481] 12.14009 12.12625 12.11296 12.10038 12.08870 12.07809 12.06873 12.06080
## [489] 12.05446 12.04991 12.04730 12.04683 12.04837 12.05160 12.05643 12.06275
## [497] 12.07045 12.07942 12.08958 12.10080 12.11298 12.12603 12.13983 12.15427
## [505] 12.16927 12.18470 12.20046 12.21646 12.23258 12.24871 12.26477 12.28063
## [513] 12.29620 12.31136 12.32602 12.34007 12.35341 12.36593 12.38037 12.39924
## [521] 12.42205 12.44832 12.47756 12.50930 12.54305 12.57834 12.61466 12.65156
## [529] 12.68853 12.72511 12.76080 12.79512 12.82760 12.85775 12.88508 12.90912
## [537] 12.92938 12.94539 12.95665 12.96571 12.97538 12.98551 12.99600 13.00671
## [545] 13.01753 13.02832 13.03897 13.04936 13.05935 13.06883 13.07767 13.08575
## [553] 13.09294 13.09913 13.10418 13.10798 13.11040 13.11132 13.11061 13.10816
## [561] 13.10383 13.09750 13.08845 13.07619 13.06101 13.04319 13.02302 13.00076
## [569] 12.97670 12.95112 12.92430 12.89652 12.86806 12.83921 12.81023 12.78141
## [577] 12.75303 12.72537 12.69871 12.67333 12.64951 12.62753 12.60767 12.58671
## [585] 12.56153 12.53258 12.50030 12.46514 12.42754 12.38795 12.34681 12.30457
## [593] 12.26167 12.21856 12.17569 12.13348 12.09240 12.05289 12.01539 11.98035
## [601] 11.94821 11.91941 11.89440 11.87364 11.85455 11.83442 11.81343 11.79175
## [609] 11.76957 11.74707 11.72442 11.70180 11.67941 11.65740 11.63598 11.61531
## [617] 11.59558 11.57696 11.55964 11.54379 11.52960 11.51725 11.50691 11.49877
## [625] 11.49301 11.48970 11.48866 11.48972 11.49269 11.49741 11.50368 11.51133
## [633] 11.52018 11.53005 11.54076 11.55212 11.56396 11.57611 11.58837 11.60057
## [641] 11.61252 11.62406 11.63500 11.64515 11.65589 11.66858 11.68304 11.69909
## [649] 11.71655 11.73524 11.75498 11.77560 11.79691 11.81873 11.84089 11.86320
## [657] 11.88549 11.90758 11.92928 11.95042 11.97081 11.99029 12.00866 12.02575
## [665] 12.04138 12.05654 12.07228 12.08856 12.10533 12.12256 12.14018 12.15815
## [673] 12.17643 12.19497 12.21372 12.23264 12.25167 12.27078 12.28991 12.30901
## [681] 12.32804 12.34696 12.36571 12.38425 12.40253 12.42051 12.43842 12.45655
## [689] 12.47487 12.49336 12.51203 12.53085 12.54981 12.56889 12.58809 12.60739
## [697] 12.62678 12.64623 12.66575 12.68531 12.70490 12.72451 12.74413 12.76374
## [705] 12.78332 12.80287 12.82237 12.84174 12.86092 12.87994 12.89882 12.91759
## [713] 12.93627 12.95489 12.97346 12.99202 13.01059 13.02919 13.04784 13.06657
## [721] 13.08541 13.10438 13.12350
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 723)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.01856 12.01375 12.00902 12.00438 11.99981 11.99532 11.99090 11.98656
##   [9] 11.98228 11.97807 11.97391 11.96982 11.96578 11.96180 11.95787 11.95398
##  [17] 11.95014 11.94635 11.94259 11.93886 11.93518 11.93152 11.92789 11.92428
##  [25] 11.92070 11.91714 11.91359 11.91006 11.90654 11.90302 11.89951 11.89601
##  [33] 11.89250 11.88899 11.88547 11.88195 11.87841 11.87486 11.87129 11.86770
##  [41] 11.86409 11.86046 11.85679 11.85308 11.84933 11.84554 11.84171 11.83786
##  [49] 11.83399 11.83011 11.82623 11.82234 11.81847 11.81461 11.81077 11.80696
##  [57] 11.80319 11.79946 11.79578 11.79216 11.78860 11.78512 11.78170 11.77838
##  [65] 11.77514 11.77201 11.76897 11.76605 11.76325 11.76057 11.75803 11.75562
##  [73] 11.75336 11.75126 11.74931 11.74753 11.74592 11.74449 11.74325 11.74221
##  [81] 11.74136 11.74072 11.74030 11.74010 11.74013 11.74017 11.74004 11.73975
##  [89] 11.73932 11.73876 11.73810 11.73735 11.73654 11.73567 11.73477 11.73387
##  [97] 11.73296 11.73208 11.73125 11.73048 11.72978 11.72919 11.72871 11.72837
## [105] 11.72818 11.72816 11.72834 11.72872 11.72933 11.73019 11.73132 11.73272
## [113] 11.73443 11.73646 11.73883 11.74155 11.74466 11.74815 11.75206 11.75640
## [121] 11.76119 11.76645 11.77219 11.77844 11.78522 11.79253 11.80139 11.81259
## [129] 11.82584 11.84086 11.85734 11.87500 11.89355 11.91271 11.93218 11.95167
## [137] 11.97090 11.98957 12.00739 12.02407 12.03934 12.05288 12.06728 12.08507
## [145] 12.10591 12.12944 12.15534 12.18324 12.21282 12.24372 12.27560 12.30812
## [153] 12.34093 12.37369 12.40605 12.43768 12.46823 12.49734 12.52469 12.54993
## [161] 12.57270 12.59267 12.60950 12.62514 12.64170 12.65910 12.67722 12.69597
## [169] 12.71526 12.73498 12.75502 12.77530 12.79572 12.81616 12.83654 12.85676
## [177] 12.87671 12.89629 12.91541 12.93397 12.95186 12.96899 12.98526 13.00057
## [185] 13.01482 13.02791 13.03973 13.05020 13.05921 13.06666 13.07245 13.07648
## [193] 13.07866 13.07888 13.07705 13.07306 13.06619 13.05597 13.04269 13.02664
## [201] 13.00810 12.98737 12.96473 12.94046 12.91486 12.88821 12.86080 12.83292
## [209] 12.80485 12.77689 12.74931 12.72241 12.69648 12.67180 12.64866 12.62735
## [217] 12.60816 12.58729 12.56124 12.53075 12.49653 12.45931 12.41982 12.37878
## [225] 12.33692 12.29496 12.25362 12.21364 12.17573 12.14062 12.10903 12.08170
## [233] 12.05935 12.03935 12.01869 11.99746 11.97576 11.95368 11.93132 11.90879
## [241] 11.88616 11.86356 11.84106 11.81877 11.79679 11.77522 11.75414 11.73366
## [249] 11.71388 11.69489 11.67679 11.65967 11.64365 11.62880 11.61549 11.60392
## [257] 11.59392 11.58536 11.57809 11.57195 11.56679 11.56248 11.55885 11.55576
## [265] 11.55307 11.55062 11.54826 11.54584 11.54323 11.54026 11.53678 11.53266
## [273] 11.52774 11.52187 11.51490 11.50822 11.50321 11.49970 11.49750 11.49646
## [281] 11.49640 11.49715 11.49855 11.50041 11.50257 11.50486 11.50711 11.50915
## [289] 11.51080 11.51190 11.51227 11.51175 11.51016 11.50734 11.50311 11.49730
## [297] 11.48979 11.48070 11.47020 11.45848 11.44572 11.43210 11.41779 11.40297
## [305] 11.38783 11.37255 11.35730 11.34226 11.32762 11.31355 11.30024 11.28786
## [313] 11.27659 11.26661 11.25810 11.24897 11.23720 11.22308 11.20691 11.18899
## [321] 11.16963 11.14910 11.12773 11.10579 11.08359 11.06143 11.03961 11.01842
## [329] 10.99817 10.97914 10.96164 10.94597 10.93242 10.92129 10.91288 10.90749
## [337] 10.90542 10.90588 10.90785 10.91119 10.91577 10.92149 10.92821 10.93581
## [345] 10.94417 10.95317 10.96268 10.97258 10.98275 10.99307 11.00341 11.01365
## [353] 11.02503 11.03876 11.05468 11.07262 11.09242 11.11392 11.13696 11.16137
## [361] 11.18699 11.21365 11.24120 11.26948 11.29831 11.32753 11.35699 11.38652
## [369] 11.41596 11.44514 11.47390 11.50209 11.52953 11.55606 11.58152 11.60575
## [377] 11.62859 11.64986 11.67178 11.69638 11.72325 11.75200 11.78223 11.81353
## [385] 11.84551 11.87776 11.90988 11.94148 11.97214 12.00147 12.02908 12.05455
## [393] 12.07748 12.09748 12.11644 12.13640 12.15722 12.17875 12.20083 12.22331
## [401] 12.24604 12.26886 12.29162 12.31418 12.33637 12.35805 12.37906 12.39925
## [409] 12.41846 12.43656 12.45337 12.46876 12.48257 12.49464 12.50482 12.51335
## [417] 12.52061 12.52667 12.53163 12.53555 12.53853 12.54062 12.54193 12.54252
## [425] 12.54248 12.54188 12.54081 12.53934 12.53756 12.53554 12.53336 12.53110
## [433] 12.52885 12.52667 12.52466 12.52289 12.52016 12.51534 12.50861 12.50017
## [441] 12.49020 12.47889 12.46642 12.45298 12.43875 12.42393 12.40869 12.39323
## [449] 12.37773 12.36237 12.34735 12.33284 12.31904 12.30614 12.29431 12.28374
## [457] 12.27463 12.26552 12.25497 12.24315 12.23024 12.21641 12.20183 12.18668
## [465] 12.17114 12.15537 12.13955 12.12386 12.10847 12.09355 12.07928 12.06583
## [473] 12.05337 12.04209 12.03216 12.02374 12.01554 12.00624 11.99600 11.98497
## [481] 11.97330 11.96115 11.94867 11.93602 11.92335 11.91081 11.89856 11.88675
## [489] 11.87554 11.86507 11.85551 11.84700 11.83971 11.83378 11.82936 11.82662
## [497] 11.82571 11.82626 11.82778 11.83019 11.83345 11.83750 11.84227 11.84771
## [505] 11.85376 11.86035 11.86743 11.87494 11.88282 11.89101 11.89945 11.90808
## [513] 11.91685 11.92569 11.93454 11.94335 11.95205 11.96058 11.97074 11.98412
## [521] 12.00037 12.01915 12.04013 12.06296 12.08729 12.11279 12.13911 12.16591
## [529] 12.19285 12.21959 12.24579 12.27109 12.29517 12.31767 12.33827 12.35660
## [537] 12.37234 12.38514 12.39466 12.40317 12.41308 12.42421 12.43639 12.44946
## [545] 12.46324 12.47756 12.49225 12.50715 12.52208 12.53688 12.55137 12.56538
## [553] 12.57875 12.59130 12.60287 12.61328 12.62237 12.62996 12.63589 12.63998
## [561] 12.64207 12.64199 12.63975 12.63561 12.62972 12.62223 12.61331 12.60310
## [569] 12.59176 12.57946 12.56634 12.55255 12.53827 12.52364 12.50882 12.49396
## [577] 12.47923 12.46477 12.45075 12.43731 12.42462 12.41283 12.40210 12.39000
## [585] 12.37423 12.35517 12.33319 12.30867 12.28198 12.25348 12.22357 12.19260
## [593] 12.16094 12.12899 12.09709 12.06564 12.03499 12.00553 11.97763 11.95165
## [601] 11.92798 11.90699 11.88904 11.87451 11.86145 11.84773 11.83348 11.81880
## [609] 11.80382 11.78866 11.77343 11.75826 11.74326 11.72855 11.71425 11.70048
## [617] 11.68735 11.67499 11.66352 11.65304 11.64369 11.63558 11.62883 11.62355
## [625] 11.61987 11.61814 11.61853 11.62085 11.62489 11.63048 11.63742 11.64552
## [633] 11.65459 11.66444 11.67488 11.68572 11.69676 11.70783 11.71873 11.72926
## [641] 11.73924 11.74848 11.75678 11.76396 11.77113 11.77945 11.78881 11.79910
## [649] 11.81021 11.82203 11.83444 11.84734 11.86062 11.87417 11.88786 11.90160
## [657] 11.91527 11.92876 11.94196 11.95476 11.96705 11.97872 11.98965 11.99973
## [665] 12.00886 12.01764 12.02672 12.03607 12.04567 12.05548 12.06548 12.07563
## [673] 12.08591 12.09629 12.10674 12.11723 12.12773 12.13820 12.14864 12.15899
## [681] 12.16924 12.17935 12.18930 12.19905 12.20858 12.21785 12.22703 12.23626
## [689] 12.24555 12.25488 12.26425 12.27365 12.28308 12.29252 12.30197 12.31141
## [697] 12.32085 12.33028 12.33969 12.34906 12.35840 12.36769 12.37693 12.38611
## [705] 12.39522 12.40425 12.41321 12.42202 12.43067 12.43915 12.44750 12.45571
## [713] 12.46381 12.47181 12.47972 12.48756 12.49535 12.50310 12.51082 12.51852
## [721] 12.52623 12.53396 12.54172
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")